package com.dinzeer.sjapadder.sa;

import com.dinzeer.legendreliclib.lib.util.FastMakeEntityUtil;
import com.dinzeer.legendreliclib.lib.util.WaitingTick;
import com.dinzeer.sjapadder.entity.DriveEntityLightning;
import com.dinzeer.sjapadder.register.SjaStacksReg;
import mods.flammpfeil.slashblade.SlashBlade;
import mods.flammpfeil.slashblade.capability.concentrationrank.ConcentrationRankCapabilityProvider;
import mods.flammpfeil.slashblade.item.ItemSlashBlade;
import mods.flammpfeil.slashblade.slasharts.Drive;
import mods.flammpfeil.slashblade.util.KnockBacks;
import mods.flammpfeil.slashblade.util.VectorHelper;
import net.minecraft.core.particles.ParticleOptions;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.damagesource.DamageTypes;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.ai.attributes.Attributes;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.Vec3;

import java.util.List;

import static com.dinzeer.sjapadder.Config.xrossThunderDamage;

public class XrossThunder {

    public static void DoSlash(LivingEntity entity){
        if (entity.level().isClientSide)return;

        Drive.doSlash(entity,-22,20,Vec3.ZERO,true,xrossThunderDamage / 4,3f);
        Drive.doSlash(entity,22,20,Vec3.ZERO,true,xrossThunderDamage / 4,3f);


        var range = 20;
        Vec3 start = entity.getEyePosition();
        Vec3 end = start.add(entity.getForward().scale(range));


        AABB boundingBox = entity.getBoundingBox().expandTowards(end.subtract(start));

        List<LivingEntity> entities = entity.level().getEntitiesOfClass(LivingEntity.class, boundingBox);


        for (LivingEntity target : entities) {
            target.hurt( FastMakeEntityUtil.DamageSourceToCreat(entity, DamageTypes.MAGIC), (float) entity.getAttributeValue(Attributes.ATTACK_DAMAGE));
        }


        Vec3 vec3 = entity.getLookAngle().normalize();
        for (int i = 0; i < range; i++) {
            var vec32 = vec3.scale(i).add(entity.getEyePosition());

         spawnParticles(entity.level(), ParticleTypes.SONIC_BOOM, vec32.x, vec32.y, vec32.z, 1, 0, 0, 0, 0, false);
        }


        WaitingTick.schedule(5, () -> {
            if (entity instanceof Player player){
                if (SjaStacksReg.BlueDreamChargerSuper.getCurrentStacks(player)>=1){
                    SjaStacksReg.BlueDreamChargerSuper.addStacks(player,-1);
                    doSlash(entity,-22,20,Vec3.ZERO,true,xrossThunderDamage / 4,3f);
                    doSlash(entity,22,20,Vec3.ZERO,true,xrossThunderDamage / 4,3f);
                }
            }
        });












    }
    public static void spawnParticles(Level level, ParticleOptions particle, double x, double y, double z, int count, double deltaX, double deltaY, double deltaZ, double speed, boolean force) {
        level.getServer().getPlayerList().getPlayers().forEach(player -> ((ServerLevel) level).sendParticles(player, particle, force, x, y, z, count, deltaX, deltaY, deltaZ, speed));
    }

















    public static DriveEntityLightning doSlash(LivingEntity playerIn, float roll, int lifetime, Vec3 centerOffset, boolean critical, double damage, float speed) {
        return doSlash(playerIn, roll, lifetime, centerOffset, critical, damage, KnockBacks.cancel, speed);
    }

    public static DriveEntityLightning doSlash(LivingEntity playerIn, float roll, int lifetime, Vec3 centerOffset, boolean critical, double damage, KnockBacks knockback, float speed) {
        int colorCode = (Integer)playerIn.getMainHandItem().getCapability(ItemSlashBlade.BLADESTATE).map((state) -> state.getColorCode()).orElse(-13421569);
        return doSlash(playerIn, roll, lifetime, colorCode, centerOffset, critical, damage, knockback, speed);
    }

    public static DriveEntityLightning doSlash(LivingEntity playerIn, float roll, int lifetime, int colorCode, Vec3 centerOffset, boolean critical, double damage, KnockBacks knockback, float speed) {
        if (playerIn.level().isClientSide()) {
            return null;
        } else {
            Vec3 lookAngle = playerIn.getLookAngle();
            Vec3 pos = playerIn.position().add((double)0.0F, (double)playerIn.getEyeHeight() * (double)0.75F, (double)0.0F).add(lookAngle.scale((double)0.3F));
            pos = pos.add(VectorHelper.getVectorForRotation(-90.0F, playerIn.getViewYRot(0.0F)).scale(centerOffset.y)).add(VectorHelper.getVectorForRotation(0.0F, playerIn.getViewYRot(0.0F) + 90.0F).scale(centerOffset.z)).add(lookAngle.scale(centerOffset.z));
            DriveEntityLightning drive = new DriveEntityLightning(SlashBlade.RegistryEvents.Drive, playerIn.level());
            drive.setPos(pos.x, pos.y, pos.z);
            drive.setDamage(damage);
            drive.setSpeed(speed);
            drive.shoot(lookAngle.x, lookAngle.y, lookAngle.z, drive.getSpeed(), 0.0F);
            drive.setOwner(playerIn);
            drive.setRotationRoll(roll);
            drive.setColor(colorCode);
            drive.setIsCritical(critical);
            drive.setKnockBack(knockback);
            drive.setLifetime((float)lifetime);
            if (playerIn != null) {
                playerIn.getCapability(ConcentrationRankCapabilityProvider.RANK_POINT).ifPresent((rank) -> drive.setRank(rank.getRankLevel(playerIn.level().getGameTime())));
            }

            playerIn.level().addFreshEntity(drive);
            return drive;
        }
    }
}
